package es.ats.codington.festival.daos;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

import es.ats.codington.festival.domain.Event;
import es.ats.codington.festival.domain.EventSignUp;
import es.ats.codington.festival.exceptions.DAOException;
import es.ats.codington.festival.interfaces.daos.IEventSignUpDAO;
import es.ats.codington.festival.utils.FERSDataConnection;
import es.ats.codington.festival.utils.FERSDbQuery;

public class EventSignUpDAOImpl implements IEventSignUpDAO {
	
	private Connection conn;
	/**
	 * Default Constructor for EventSignUpDAOImpl
	 * @param conn
	 */
	public EventSignUpDAOImpl(Connection conn) {
		this.conn = conn;
	}
	
	/**
	 * Obtain a list of all the Events
	 * @param resultSet
	 * @return events
	 * @throws SQLException
	 */
	private List<Event> getEventList(ResultSet resultSet) throws SQLException {
		List<Event> events = new ArrayList<Event>();
		
		while(resultSet.next()) {
			Event event = new Event();
			
			event.setEventID(resultSet.getInt("eventId"));
			event.setName(resultSet.getString("name"));
			event.setDescription(resultSet.getString("description"));
			event.setPlace(resultSet.getString("place"));
			event.setStartEvent(resultSet.getString("startEvent"));
			event.setEndEvent(resultSet.getString("endEvent"));
			event.setDayEvent(resultSet.getString("dayEvent"));
			event.setEventType(resultSet.getString("eventType"));
			event.setSeatsAvailable(resultSet.getInt("seatsAvailable"));
			event.setIdPlace(resultSet.getInt("idPlace"));
			
			events.add(event);
		}
		return events;
	}
	
	/**
	 * Obtain the list of all the events in which the visitor is registered in.
	 * @param eventSignUp 
	 * @return check
	 * @throws DAOException
	 */
	@Override
	public boolean checkEventsofVisitor(EventSignUp eventSignUp) throws DAOException {
		
		boolean check = false;
		List<EventSignUp> eventSignUpList = new ArrayList<EventSignUp>();
		PreparedStatement ps = null;
		ResultSet rs = null;
		String sql = FERSDbQuery.getCheckEvent();
		
		
		try {
			ps = conn.prepareStatement(sql);
			ps.setInt(1, eventSignUp.getIdEvent());
			ps.setInt(2, eventSignUp.getIdVisitor());
			
			rs = ps.executeQuery();
			while(rs.next()) {
				EventSignUp eventSignUpTemp = new EventSignUp();
				
				eventSignUpTemp.setId(rs.getInt("id"));
				eventSignUpTemp.setIdEvent(rs.getInt("idEvent"));
				eventSignUpTemp.setIdVisitor(rs.getInt("idVisitor"));
				
				eventSignUpList.add(eventSignUpTemp);
				
			}
			if(eventSignUpList.size() == 1) {
				check = true;
			}
		}  catch(SQLException ex) {
			
			throw new DAOException(ex.getMessage(), ex);
			
		} finally{
			
			FERSDataConnection.closeStatement(ps);
			FERSDataConnection.closeResultSet(rs);
		}
		return check;
	}
	
	/**
	 * let the availability to the Visitor to unregister from an event
	 * @param eventSignUp
	 * @return deleteVisitorToEvent
	 * @throws DAOException
	 */
	@Override
	public boolean unregisterVisitorToEvent(EventSignUp eventSignUp) throws DAOException {
		
		boolean deleteVisitorToEvent = false;
		Integer affectedRows = 0;
		PreparedStatement ps = null;
		String sql = FERSDbQuery.getUnRegisterVisitorToEvent();
		
		try{
			ps = conn.prepareStatement(sql);
			ps.setInt(1, eventSignUp.getIdEvent());
			ps.setInt(2, eventSignUp.getIdVisitor());
			
			affectedRows = ps.executeUpdate();
			}
		catch (SQLException ex) {
			
			throw new DAOException(ex.getMessage(), ex);
			
		} finally{
			
			FERSDataConnection.closeStatement(ps);
		}
		
		if (affectedRows > 0) {
			deleteVisitorToEvent = true;
		}
		
		return deleteVisitorToEvent;
	}
		
	/**
	 * events which the visitors are registered in or wants to do it.
	 * @param eventsignup
	 * @return insertVisitorToEvent
	 * @throws DAOException
	 */
	@Override
	public boolean registerVisitorToEvent(EventSignUp eventsignup) throws DAOException {
		
		boolean insertVisitorToEvent = false;
		Integer affectedRows = 0;
		PreparedStatement ps = null;
		String sql = FERSDbQuery.getRegisterVisitorToEvent();
		
		try {
		
			ps = conn.prepareStatement(sql);
			//ps.setInt(1, eventSignUp.getId());
			ps.setInt(1, eventsignup.getIdEvent());
			ps.setInt(2, eventsignup.getIdVisitor());
			
			affectedRows= ps.executeUpdate();
			
		} catch(SQLException ex) {
			
			throw new DAOException(ex.getMessage(), ex);
			
		} finally{
			
			FERSDataConnection.closeStatement(ps);
		}
		
		if (affectedRows > 0) {
			insertVisitorToEvent = true;
		}
		
		return insertVisitorToEvent;
	}
	
	/**
	 * events which the visitors are registered in.
	 * @param eventSignUp
	 * @return events
	 * @throws DAOException
	 */
	@Override
	public List<Event> registeredEvents(EventSignUp eventSignUp) throws DAOException {
		List<Event> events = new ArrayList<Event>();
		PreparedStatement ps = null;
		ResultSet rs = null;
		String sql = FERSDbQuery.getEventsRegistered();
		
		try {
			ps = conn.prepareStatement(sql);
			ps.setInt(1, eventSignUp.getIdVisitor());
			
			rs = ps.executeQuery();
			
			events = this.getEventList(rs);
			
		} catch(SQLException ex) {
			throw new DAOException(ex.getMessage(), ex);
		} finally {
			FERSDataConnection.closeStatement(ps);
			FERSDataConnection.closeResultSet(rs);
		}
		return events;
		
	}
	
	/*public static void main(String[] args){
		Connection conn;
		EventSignUpDAOImpl eventSignUpDAO;	
		EventSignUp eventSignUp1;
		
		conn = FERSDataConnection.getConnection();
		eventSignUpDAO = new EventSignUpDAOImpl(conn);
		
		boolean affectedRows;
        eventSignUp1 = new EventSignUp();
		
		eventSignUp1.setIdEvent(1);
		eventSignUp1.setIdVisitor(5);
		
		affectedRows = eventSignUpDAO.registerVisitorToEvent(eventSignUp1);
		try {
			FERSDataConnection.commit(conn);
			FERSDataConnection.closeConnection(conn);
		}	catch (DAOException ex) {System.out.println("DAO Exception " + ex.getMessage());}
		
	}*/
		
	
		
	}
	
